package com.iweipan.activity;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.iweipan.R;
import com.iweipan.base.WPBaseActivity;
import com.iweipan.utils.sputils.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

import static com.iweipan.base.WPUrlBase.WP_IS_REGSISTER;

/**
 * Created by Administrator on 2016/11/22.
 */

public class WebViewActivity extends WPBaseActivity {

    private WebView webView;

    @Override
    public int bindLayout() {
        return R.layout.activity_webview;
    }

    @Override
    public void initToolbarColor() {
        color = getResources().getColor(R.color.actionbar_title_bg);
        statusBarAlpha = 60;
    }

    @Override
    public void initView(View view) {
        webView = (WebView) view.findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        String mobile = getIntent().getStringExtra("mobile");
        initIsRegister(mobile);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("WebViewActivity", url);
                if (url.contains("?token=")) {
                    String wp_token = url.substring(20);
                    SPUtils.put(WebViewActivity.this, "wp_isLogin", true);
                    SPUtils.put(WebViewActivity.this, "wp_token", wp_token);
                    Log.e("WebViewActivity", wp_token);
                    finish();
                } else {
                    view.loadUrl(url);
                }

                return true;
            }


        });
    }

    @Override
    public void doBusiness(Context mContext) {
//        BuyDialogFragment buyDialogFragment = new BuyDialogFragment();
//        buyDialogFragment.show(getSupportFragmentManager(), "");
    }

    @Override
    public void resume() {

    }


    private void initIsRegister(final String moblie) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", moblie);
            jsonObject.put("mid", "016");
            OkHttpUtils.get()
                    .url(WP_IS_REGSISTER)
                    .addParams("p", jsonObject.toString())
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("LoginActivity", e.toString());
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            try {
                                Log.d("LoginActivity", response);
                                JSONObject request = new JSONObject(response);
                                if (request.get("code").equals("200")) {
                                    webView.loadUrl("http://112.74.212.112:8090/app-api/"+"register_rt.html"+"?mobile=" + moblie + "&callback=http://weipan.izhipeng.com/callBackRegister.jspa&rt=http://0.com&mid=016");
                                } else if (request.get("code").equals("1030")) {
                                    webView.loadUrl("http://112.74.212.112:8090/app-api/"+"login_rt.html"+"?mobile=" + moblie + "&callback=http://weipan.izhipeng.com/callBackRegister.jspa&rt=http://0.com&mid=016");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void destroy() {
        if (webView != null) {
            webView.destroy();
        }

    }
}
